package com.dgut.doctorservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.Doctor;
import com.dgut.doctorservice.entity.Schedule;
import com.dgut.doctorservice.service.DoctorService;
import com.dgut.doctorservice.service.ScheduleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/doctorservice/schedule")
@CrossOrigin
public class ScheduleController {

    @Autowired
    ScheduleService scheduleService;
    @Autowired
    DoctorService doctorService;

    @ApiOperation("返回列表数据，添加排班信息")
    @PostMapping("setSchedule")
    public Result setSchedule(@RequestBody List<Schedule> schedules){
        for (int i = 0; i <schedules.size() ; i++) {
            QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
            wrapper.eq("date",schedules.get(i).getDate());
            wrapper.eq("d_id",schedules.get(i).getDId());
            List<Schedule> list = scheduleService.list(wrapper);
            if(list.size()==0){
                scheduleService.save(schedules.get(i));
            }else {
                scheduleService.updateById(schedules.get(i));
            }
        }
        return Result.succ();
    }

    @ApiOperation("获取所有排班信息")
    @PostMapping("getSchedule")
    public Result getSchedule(){
        List<Schedule> list = scheduleService.list();
        return Result.succ().data("list",list);
    }

    @ApiOperation("获取某个医生的排班信息")
    @PostMapping("getScheduleById")
    public Result getScheduleById(@RequestParam String id){
        QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
        wrapper.eq("d_id",id);
        List<Schedule> list = scheduleService.list(wrapper);
        List<Doctor> doctors=new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            Doctor one = doctorService.getById(list.get(i).getDId());
//            QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("id",list.get(i).getDId());
//            Doctor one = doctorService.getOne(queryWrapper);
            doctors.add(one);
        }
        return Result.succ().data("list",list).data("doctors",doctors);
    }

    @ApiOperation("获取某个科室医生排班信息")
    @PostMapping("getScheduleByDepartmentId")
    public Result getScheduleByDepartmentId(@RequestParam String departmentId){
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();
        wrapper.eq("department_id",departmentId);
        List<Doctor> list = doctorService.list(wrapper);
        List<Schedule> schedules=new ArrayList<>();
        //HashMap<String, List<Schedule>> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            QueryWrapper<Schedule> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("d_id",list.get(i).getId());
            List<Schedule> list1 = scheduleService.list(wrapper1);
            schedules.addAll(list1);
            //map.put(list.get(i).getId(),list1);
        }
        //return Result.succ().data("list",list).data("schedule",map);
        return Result.succ().data("list",list).data("schedules",schedules);
    }

}

